package com.seeyon.apps.kk.listener.form;

import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.listener.BaseListener;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.apps.kk.manager.LockBusinessTravelSapManager;
import com.seeyon.apps.kk.manager.LockCheckworkSapManager;
import com.seeyon.apps.kk.manager.LockOvertimeSapManager;
import com.seeyon.apps.kk.manager.LockVacationSapManager;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.cap4.form.modules.event.FormDataAfterSubmitEvent;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.kk.utils.ValueUtils;

public class ThirdPushLogListener extends BaseListener {
	private static Log log = LogFactory.getLog(ThirdPushLogListener.class);
	private LockVacationSapManager lockmanager;
	private LockOvertimeSapManager lockovermanager;
	private LockCheckworkSapManager lockcheckmanager;
	private LockBusinessTravelSapManager lockbussimanager;
	private FreemarkFlowManager freemarkFlowManager;
	private String tablebame;

	public LockVacationSapManager getLockmanager() {
		return lockmanager;
	}

	public void setLockmanager(LockVacationSapManager lockmanager) {
		this.lockmanager = lockmanager;
	}

	public LockOvertimeSapManager getLockovermanager() {
		return lockovermanager;
	}

	public void setLockovermanager(LockOvertimeSapManager lockovermanager) {
		this.lockovermanager = lockovermanager;
	}

	public LockCheckworkSapManager getLockcheckmanager() {
		return lockcheckmanager;
	}

	public void setLockcheckmanager(LockCheckworkSapManager lockcheckmanager) {
		this.lockcheckmanager = lockcheckmanager;
	}

	public LockBusinessTravelSapManager getLockbussimanager() {
		return lockbussimanager;
	}

	public void setLockbussimanager(LockBusinessTravelSapManager lockbussimanager) {
		this.lockbussimanager = lockbussimanager;
	}

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public String getTablebame() {
		return tablebame;
	}

	public void setTablebame(String tablebame) {
		this.tablebame = tablebame;
	}

	@ListenEvent(event = FormDataAfterSubmitEvent.class, async = true)
	public void onFormDataAfterSubmit(FormDataAfterSubmitEvent event) {
		try {
			Thread.sleep(BaseListener.LISTENER_THREAD_SLEEPTIME);
			log.info("重新抛送数据：start" );
			FormDataMasterBean formDataMasterBean = (FormDataMasterBean) event.getSource();
			FormTableBean formTableBean = formDataMasterBean.getFormTable();
			log.info("重新抛送数据,模板编号：" + formTableBean.getTableName());
			if (formTableBean.getTableName().equals(this.tablebame)) {
				Map<String, Object> formdata1 = formDataMasterBean.getAllDataMap(); // 单据内容map
				Long again = MapUtils.getLong(formdata1, "field0014");// 重新抛送
				String ifshi = MapUtils.getString(formdata1, "field0001");// 单据类型
				String yuansj = MapUtils.getString(formdata1, "field0005");// 抛送源数据
				String logid = MapUtils.getString(formdata1, "id");
				JSONObject json = JSONObject.parseObject(yuansj.replaceAll(" ", ""));
				log.info("重新抛送数据,JSON:"+JSONUtil.toJSONString(json));
				Map<String, Object> rouse = json;
				log.info("重新抛送数据,Map:" + JSONUtil.toJSONString(rouse));
				// 判断是否返回为E的单据
				// 判断重新抛送的值是否为1
				if (ValueUtils.getConfig().getLong("kk.CXPS").equals(again)) {
					// 判断是否为休假单据
					if ("休假申请单".trim().equals(ifshi)) {
						Map<String, Object> sa = this.lockmanager.SendToGhr(rouse);
						Map<String, Object> mv = new HashMap<>();
						mv.put("IFDATE", MapUtils.getString(sa, "IF_DATE"));
						mv.put("STATUS", MapUtils.getString(sa, "STATUS"));
						mv.put("MESSAGER", MapUtils.getString(sa, "XIAOXI"));
						mv.put("LOGID", logid);
						String findsql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/gengxin.ftl", mv);
						this.jdbcTemplate.update(findsql);

						// 判断是否为加班单据
					} else if ("加班申请单".trim().equals(ifshi)) {
						Map<String, Object> sa = this.lockovermanager.AddOvertimeForm(rouse);
						Map<String, Object> mv = new HashMap<>();
						mv.put("IFDATE", MapUtils.getString(sa, "IF_DATE"));
						mv.put("STATUS", MapUtils.getString(sa, "STATUS"));
						mv.put("MESSAGER", MapUtils.getString(sa, "XIAOXI"));
						mv.put("LOGID", logid);
						String findsql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/gengxin.ftl", mv);
						this.jdbcTemplate.update(findsql);

						// 判断是否为考勤单据
					} else if ("考勤确认书".trim().equals(ifshi)) {
						Map<String, Object> sa = this.lockcheckmanager.addCheckwordForm(rouse);
						log.info("重新抛送数据,考勤确认书：" + JSONUtil.toJSONString(sa));
						Map<String, Object> mv = new HashMap<>();
						mv.put("IFDATE", MapUtils.getString(sa, "IF_DATE"));
						mv.put("STATUS", MapUtils.getString(sa, "STATUS"));
						mv.put("MESSAGER", MapUtils.getString(sa, "XIAOXI"));
						mv.put("LOGID", logid);
						String findsql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/gengxin.ftl", mv);
						this.jdbcTemplate.update(findsql);

						// 判断是否为出差单据
					} else if ("出差报告".trim().equals(ifshi)) {
						Map<String, Object> sa = this.lockbussimanager.SendToGhr(rouse);
						Map<String, Object> mv = new HashMap<>();
						mv.put("IFDATE", MapUtils.getString(sa, "IF_DATE"));
						mv.put("STATUS", MapUtils.getString(sa, "STATUS"));
						mv.put("MESSAGER", MapUtils.getString(sa, "XIAOXI"));
						mv.put("LOGID", logid);
						String findsql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/gengxin.ftl", mv);
						this.jdbcTemplate.update(findsql);
					}
				}
			}
			log.info("重新抛送数据：end");
		} catch (Exception e) {
			log.error("重新抛送GHR数据错误：" + e.getMessage());
			log.error(e);
		}
	}
}